package com.yanxinim.cn.ui.me.redpacket;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.callback.ListCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ArrayResult;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.BankCardAdapter;
import com.yanxinim.cn.bean.redpacket.BankCardBean;
import com.yanxinim.cn.call.CustomOnItemClickListener;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.util.ToastUtil;
import com.yanxinim.cn.view.AlertDialog;
import com.yanxinim.cn.view.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.Nullable;
import okhttp3.Call;

/**
 * @ClassName BankCardListActivity
 * @Description 银行卡列表页面
 * @Author ChenXL
 * @Date 2021/5/25 19:05
 * @Version 1.0
 */
public class BankCardListActivity extends BaseActivity {

    public static final int ADDBANDCARD_REQUEST_CODE = 0x100;

    private MyListView mlvBankCard;
    private TextView tvAddCard;

    private BankCardAdapter adapter;

    private List<BankCardBean> beanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card_list);
        initActionBar();
        initView();
        initData();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText(getString(R.string.bank_card));
    }

    private void initView(){
        mlvBankCard = findViewById(R.id.mlvBankCard);
        tvAddCard = findViewById(R.id.tvAddBankCard);

        adapter = new BankCardAdapter(this);
        // 绑定适配器
        mlvBankCard.setAdapter(adapter);

        mlvBankCard.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        adapter.setOnItemClickListener(new CustomOnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                BankCardBean bean = (BankCardBean) view.getTag();
                if (bean == null){
                    return;
                }
                String bankId = bean.getId();
                if (TextUtils.isEmpty(bankId)){
                    return;
                }
                showRemoveBankAlart(bankId);
            }
        });

        tvAddCard.setOnClickListener(view -> {
            Intent addCardIntent = new Intent(BankCardListActivity.this, AddBankCardActivity.class);
            startActivityForResult(addCardIntent, ADDBANDCARD_REQUEST_CODE);
        });
    }

    private void initData(){
//        BankCardBean bean1 = new BankCardBean();
//        bean1.setUserName("张三");
//        bean1.setCardNo("62222222222222221234");
//        BankCardBean bean2 = new BankCardBean();
//        bean2.setUserName("李四");
//        bean2.setCardNo("62222222222222225678");
//        beanList.add(bean1);
//        beanList.add(bean2);

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);

        HttpUtils.get().url(coreManager.getConfig().MY_BANK_LIST)
                .params(params)
                .build()
                .execute(new ListCallback<BankCardBean>(BankCardBean.class) {

                    @Override
                    public void onResponse(ArrayResult<BankCardBean> result) {
                        if (result.getResultCode() == 1 && result.getData() != null) {
                            beanList = result.getData();
                            if (beanList.size() != 0) {
                                adapter.setDatas(beanList);
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            ToastUtil.showErrorData(BankCardListActivity.this);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtil.showNetError(BankCardListActivity.this);
                    }
                });

    }

    /**
     * 移除银行卡弹框
     */
    private void showRemoveBankAlart(final String bankId){
        new AlertDialog(BankCardListActivity.this).builder().setTitle("解除绑定")
                .setMsg("您确认要解除绑定该银行卡？若解除绑定，此后将不能使用该银行卡充值和提现。")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeBank(bankId);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    /**
     * 移除银行卡
     */
    private void removeBank(String bankId){
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("id", bankId);

        HttpUtils.get().url(coreManager.getConfig().BANK_REMOVE)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {

                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        if (result.getResultCode() == 1 ) {
                            ToastUtil.showToast(BankCardListActivity.this, "解除绑定成功");
//                            setResult(RESULT_OK);
//                            finish();
                            //刷新数据
                            initData();
                        } else {
                            ToastUtil.showErrorData(BankCardListActivity.this);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtil.showNetError(BankCardListActivity.this);
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADDBANDCARD_REQUEST_CODE && resultCode == RESULT_OK){
            initData();
        }
    }
}
